﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using XMLGener.Utils;

namespace XMLGener.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class XMLGenerElements : XMLGenerRangeElement
    {
        /// <summary>
        /// 
        /// </summary>
        private XMLGenerElements()
            : base()
        {}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parent"></param>
        public XMLGenerElements(String name, XMLGenerNode parent)
            : base(name, parent)
        {
            this.xtype = XNodeType.ELEMENTS;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="xmlElem"></param>
        public XMLGenerElements(XMLGenerNode parent, XmlElement xmlElem)
            : base(parent,xmlElem)
        {
            this.xtype = XNodeType.ELEMENTS;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void defaultInitialization()
        {
            base.defaultInitialization();
            this.nodeTypeStr = XMLGenerNode.XELEMENTS_STR;
        }

        /// <summary>
        /// Return avialable types for this type of node
        /// </summary>
        /// <returns>list of strings</returns>
        public override XDataTypeCollection listAvailabeTypes()
        {
            return XMLGenerElements.baseTypesList;
            /*XDataTypeCollection result = new XDataTypeCollection();
            result.Add(new XDataType("random"));
            result.Add(new XDataType("ordered"));
            return result;*/
            /*
            ArrayList result = new ArrayList();
            result.Add("random");
            result.Add("ordered");
            return result;
            */
        }

        /// <summary>
        /// 
        /// </summary>
        public override int depth
        {
            get
            {
                if (_depth < 0 && parentNode != null)
                {
                    this._depth = parentNode.depth ;
                }
                return _depth;
            }
            set { this._depth = value; }
        }

        //public String id = "";


        //---------------------------
        // attributes from xml
        //---------------------------
        

        /// <summary>
        /// Return nodes type.
        /// </summary>
        public override string contentType
        {
            get { return valideContentType( getXMLParam("type", "ordered")); }
            set { setXMLParam("type", valideContentType(value)); }
        }

        private static string valideContentType(string value)
        {
            if (value!="random")
                return "ordered";
            return value;
        }
        public override String name
        {
            get { return "elememts"; }
            set {}
        }

        private static XDataTypeCollection _baseTypesList = null;

        private static XDataTypeCollection baseTypesList
        {
            get 
            {
                if (_baseTypesList == null)
                    loadElementsTypes();
                return _baseTypesList;
            }
        }

        private static XmlDocument typesDefinition = null;
        private static void loadElementsTypes()
        { 
            typesDefinition = new XmlDocument();
            typesDefinition.LoadXml(
                "<doc><" + XMLGenerNode.XDATATYPE_STR + " name=\"random\"/>" +
                "<" + XMLGenerNode.XDATATYPE_STR + " name=\"ordered\" /></doc>"
                );
            XmlElement node = XMLUtils.getSubElement(typesDefinition.DocumentElement,XMLGenerNode.XDATATYPE_STR);
            XDataTypeCollection list = new XDataTypeCollection();
            while (node != null)
            {
                XDataType newType = new XDataType(node);
                newType.isRootType = true;
                list.Add(newType);
                node = XMLUtils.getNextElement(node, XMLGenerNode.XDATATYPE_STR);
            }
            _baseTypesList = list;
        
        }

    }
}
